﻿using System;
using System.Text;
using System.Collections.Generic;
using Demo_G.O.S.E.ServerEngine.Common;

namespace Demo_G.O.S.E.ServerEngine.GameCommand
{
    public class GameCommands
    {
        /// <summary>
        /// 
        /// </summary>
        private static Dictionary<string, BaseGameCommand> s_DictionaryGameCommands = new Dictionary<string, BaseGameCommand>();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strGameCommand"></param>
        /// <param name="iAccessLevel"></param>
        /// <param name="eventGameCommand"></param>
        public static void Register( string strGameCommand, AccessLevel iAccessLevel, EventDelegateGameCommand eventGameCommand )
        {
            BaseGameCommand l_GameCommand = null;
            s_DictionaryGameCommands.TryGetValue( strGameCommand, out l_GameCommand );

            if ( l_GameCommand == null )
            {
                l_GameCommand = new BaseGameCommand();
                l_GameCommand.m_strGameCommand = strGameCommand;
                l_GameCommand.m_iAccessLevel = iAccessLevel;
                l_GameCommand.EventGameCommand += eventGameCommand;
                s_DictionaryGameCommands.Add( strGameCommand, l_GameCommand );
            }
            else
            {
                l_GameCommand.EventGameCommand += eventGameCommand;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private static string s_strPrefix = "/";
        /// <summary>
        /// 
        /// </summary>
        private static string s_strMatchPrefix = "/ ";
        /// <summary>
        /// 
        /// </summary>
        public static string Prefix
        {
            get { return GameCommands.s_strPrefix; }
            set
            {
                s_strPrefix = value;
                s_strMatchPrefix = s_strPrefix + " ";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strGameCommand"></param>
        /// <returns></returns>
        public static BaseGameCommand GetGameCommand( string strGameCommandLine )
        {
            int iIndexOf = strGameCommandLine.IndexOf( s_strMatchPrefix );
            if ( iIndexOf != 0 )
                return null;

            iIndexOf = strGameCommandLine.IndexOf( " ", s_strMatchPrefix.Length );
            if ( iIndexOf <= 0 )
                return null;

            string strGameCommand = strGameCommandLine.Substring( s_strMatchPrefix.Length, iIndexOf - s_strMatchPrefix.Length );

            BaseGameCommand l_GameCommand = null;
            s_DictionaryGameCommands.TryGetValue( strGameCommand, out l_GameCommand );

            if ( l_GameCommand != null )
                l_GameCommand.m_strGameCommandLine = strGameCommandLine.Substring( iIndexOf + 1 );

            return l_GameCommand;
        }
    }
}

